// mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    set: 'back',
    img_src: '',
    privacy_show: false,
    min_image: app.data.host + '/plugins/ai-tongue-diagnosis/assets/images/images_standard.png',
    info: [],
    privacy_agreement_show: false,
    undercount: false,
    undercount_msg: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.ctx = wx.createCameraContext();
    this.getData();
  },
  showM() {
    wx.showModal({
      title: '隐私提醒',
      content: '为了更准确检测，我需要获取您的摄像头、相册、性别和年龄权限。',
      cancelText: '隐私协议',
      confirmText: '同意',
      confirmColor: '#FFAA01',
      complete: (res) => {
        console.log(res)
        if (res.cancel) {
          this.setData({
            privacy_show: true
          })
        }
        if (res.confirm) {
          this.setData({
            privacy_agreement_show: false
          })
        }
      }
    })
  },
  onClose() {
    this.setData({
      privacy_show: false
    })
    if (this.data.privacy_agreement_show) {
      this.showM();
    }
  },
  takePhoto_set() {
    this.setData({
      set: this.data.set == 'back' ? 'front' : 'back'
    })
  },
  takePhoto() {
    if (this.data.undercount) {
      this.showUnderCountModel();
      return
    }
    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        this.setData({
          img_src: res.tempImagePath
        })
        wx.navigateTo({
          url: '/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_information/index?img=' + this.data.img_src,
        })
      }
    })
  },
  takePhoto_set() {
    this.setData({
      set: this.data.set == 'back' ? 'front' : 'back'
    })
  },
  choose_Img() {
    if (this.data.undercount) {
      this.showUnderCountModel();
      return
    }
    wx.chooseMedia({
      count: 1,
      mediaType: 'image',
      sourceType: ["album"],
      success: (res) => {
        let tempFilePath = res.tempFiles[0].tempFilePath;
        this.setData({
          img_src: tempFilePath
        })
        wx.navigateTo({
          url: '/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_information/index?img=' + this.data.img_src,
        })
      },
      fail: (err) => {
        console.log(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  async getData() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.ai-tongue-diagnosis.frontend.controllers.detection.index"),
      success: (res) => {
        let {
          data
        } = res;
        if (data.result) {
          this.setData({
            info: data.data,
            undercount: false
          })
          if (data.data.privacy_agreement == 1) {
            this.showM();
            this.setData({
              privacy_agreement_show: true
            })
          }
        } else {
          if(data.data.standard_img){
            this.setData({
              'info.standard_img':data.data.standard_img
            })
          }
          if (data.data.status == '-1') {
            this.setData({
              undercount: true,
              undercount_msg: data.msg
            })
            this.showUnderCountModel()
            return
          }
          this.setData({
            undercount: false
          })
          app.tips(data.msg);
        }
      }
    })
  },
  showUnderCountModel() {
    wx.showModal({
      content: this.data.undercount_msg,
      complete: (res) => {
        if (res.confirm) {
          wx.navigateTo({
            url: '/mircoApp/ai-tongue-diagnosis/ai_tongue_diagnosis_joinMember/ai_tongue_diagnosis_joinMember',
          })
        }
      }
    })
  }
})